<!--  -->
<template>
  <div id="main">
    <canvas id="particle-canvas" width="1142" height="875"></canvas>
    <div class="content">
      <div class="login">
        <div class="title">Yqg酒店管理系统</div>
        <el-form
          size="mini"
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="50px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="loginId">
            <el-input v-model="ruleForm.loginId" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="loginPwd">
            <el-input
              type="password"
              v-model="ruleForm.loginPwd"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { strToMd5 } from "util/md5.js";
import { start } from "../../assets/js/login";
export default {
  name: "",
  mounted() {
    start();
  },
  data() {
    //验证账号
    var validateLoginId = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    //验证密码
    var validateLoginPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      //表单数据
      ruleForm: {
        //登录名
        loginId: "",
        //登录密码
        loginPwd: "",
      },
      //表单验证数据
      rules: {
        //验证登录名
        loginId: [{ validator: validateLoginId, trigger: "blur" }],
        //验证登录密码
        loginPwd: [{ validator: validateLoginPwd, trigger: "blur" }],
      },
    };
  },
  methods: {
    //表单的提交方法
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //实现登录
          //密码加密
          this.ruleForm.loginPwd = strToMd5(this.ruleForm.loginPwd);
          let { message, success, token } = await this.$get(
            "/Admin/Login",
            this.ruleForm
          );
          if (success) {
            //服务器返回一个token
            sessionStorage.setItem("token", token);
            //在浏览器中保存登录名
            localStorage.setItem("loginId", this.ruleForm.loginId);
            //将token保存到请求头中
            this.$setToken();
            //跳转到后台管理
            this.$router.push("/layout");
          } else {
            this.$msg_e(message);
          }
        } else {
          return false;
        }
      });
    },
    //表单重置方法
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
#particle-canvas {
  width: 100%;
  height: 100vh;
  background: linear-gradient(
    to bottom,
    rgb(10, 10, 50) 0%,
    rgb(60, 10, 60) 100%
  );
  vertical-align: middle;
}
#main {
  width: 100vw;
  height: 100vh;
  position: relative;
}
#main .content {
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
}
#main .login {
  width: 400px;
  height: 250px;
  border: 1px solid #ccc;
  text-align: center;
  padding: 30px;
  padding-right: 70px;
  border-radius: 10px;
}
#main .login .title {
  color: #fff;
  font-size: 22px;
  margin-bottom: 30px;
  font-family: dn;
}
</style>